package com.yijiu.trb;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.BaseResponse;

/**
 * 忘记支付密码
 * 
 * @author TangChao
 */
public class ForgetPwdTwoActivity extends BTitleActivity
{
	private EditText phoneEditText, newPwdEditText, newPwdAgainEditText, codeEditText;
	
	private Button sendCode;
	
	private String phoneStr;
	
	/**
	 * 手机号码
	 */
	private String mobile;
	
	private String forgetPwdType = "logPassword", flagServer = "ForgetLoginPassWord";
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_pwd_two_layout);
		mobile = getIntent().getStringExtra("mobile");
		initWidget();
	}
	
	@Override
	protected void initWidget()
	{
		titleView.setText("忘记密码");
		phoneEditText = (EditText) findViewById(R.id.et_forget_pwd_two_phone_num);
		phoneEditText.setText(mobile);
		phoneEditText.setFocusable(false);
		newPwdEditText = (EditText) findViewById(R.id.et_forget_pwd_two_new_pwd);
		newPwdAgainEditText = (EditText) findViewById(R.id.et_forget_pwd_two_new_pwd_again);
		codeEditText = (EditText) findViewById(R.id.et_forget_pwd_two_code);
		sendCode = (Button) findViewById(R.id.btn_forget_pwd_two_code_send);
		if ("pay".equals(getIntent().getStringExtra("flag")))
		{
			forgetPwdType = "payPassword";
			flagServer = "ForgetPayPassWord";
			newPwdEditText.setHint("请输入新支付密码");
			newPwdAgainEditText.setHint("请再次输入新支付密码");
		}
	}
	
	private void doDataCheck()
	{
		String newPwdStr = newPwdEditText.getText().toString();
		String newPwdAgainStr = newPwdAgainEditText.getText().toString();
		String codeStr = codeEditText.getText().toString();
		if (StringUtils.isNull(newPwdStr))
		{
			app.showMsg("请输入新密码");
			return;
		}
		if (StringUtils.isNull(newPwdAgainStr))
		{
			app.showMsg("请再次输入新密码");
			return;
		}
		if (!newPwdAgainStr.equals(newPwdStr))
		{
			app.showMsg("两次输入的密码不一样");
			return;
		}
		if (newPwdStr.length() < 6 || newPwdStr.length() > 16 || !StringUtils.checkHasLetNum(newPwdStr)
				|| !StringUtils.checkHasLetNum(newPwdAgainStr))
		{
			app.showMsg("密码应该为6~16位字符，至少包含数字、字母（区分大小写）");
			return;
		}
		if (StringUtils.isNull(codeStr))
		{
			app.showMsg("请输入验证码");
			return;
		}
		String inputStr = "service=forgotPasswordService&newPassword=" + newPwdAgainStr + "&userName="
				+ getIntent().getStringExtra("userName") + "&pwdType=" + forgetPwdType + "&verifyCode=" + codeStr
				+ "&mobile=" + phoneStr + "&business=" + flagServer;
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction baseAction = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		BaseResponse baseResponse = new BaseResponse();
		Request r = new Request(baseAction, baseResponse, ActionType.FORGET_PASSWORD);
		netManager.excute(r, this, this);
		showProgress("正在找回密码，请稍候！");
	}
	
	/**
	 * 发送验证码
	 */
	private void doSendAgainDataCheck()
	{
		phoneStr = phoneEditText.getText().toString();
		if (StringUtils.isNull(phoneStr))
		{
			app.showMsg("请输入手机号码");
			return;
		}
		if (!StringUtils.isPhoneNumberValid(phoneStr) || phoneStr.length() != 11)
		{
			app.showMsg("请输入有效的手机号码");
			return;
		}
		timerCount(sendCode);
		String inputStr = "service=appMobileService&mobile=" + phoneStr + "&business=" + flagServer;
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction baseAction = new BaseAction(encodedData, encriptData);
		BaseResponse baseResponse = new BaseResponse();
		Request r = new Request(baseAction, baseResponse, ActionType.SEND_AGAIN);
		netManager.excute(r, this, this);
		showProgress("正在发送验证码，请稍候！");
	}
	
	/* 操作成功返回的函数 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.SEND_AGAIN:
				app.showMsg("发送验证码成功！");
				break;
			case ActionType.FORGET_PASSWORD:
				String aString = getIntent().getStringExtra("flag");
				if ("pay".equals(getIntent().getStringExtra("flag")))
				{
					app.showMsg("您的新支付密码已设置成功！");
					// initIntent(this, AccountManagerActivity.class);
					finish();
				}
				else
				{
					app.showMsg("您的新登录密码已设置成功！");
					initIntent(this, LoginActivity.class);
				}
				finish();
				break;
		}
	}
	
	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.SEND_AGAIN:
			case ActionType.FORGET_PASSWORD:
				app.showMsg(errorMsg);
				break;
		}
	}
	
	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
	
	public void forgetPwdTwoClick(View v)
	{
		int id = v.getId();
		switch (id)
		{
			case R.id.btn_forget_pwd_two_code_send:
				doSendAgainDataCheck();
				break;
			case R.id.btn_forget_pwd_two_submit:
				doDataCheck();
				break;
			default:
				break;
		}
	}
}
